package com.note.app.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.note.app.dto.NoteDTO;
import com.note.app.dto.ShareCreateDTO;
import com.note.app.dto.ShareDTO;
import com.note.app.service.ShareService;
import com.note.common.context.UserContext;
import com.note.common.vo.ResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.note.app.annotation.LogRecord;

/**
 * 分享控制器
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Tag(name = "笔记分享", description = "笔记分享相关接口")
public class ShareController {

    private final ShareService shareService;
    
    @PostMapping("/api/v1/shares")
    @Operation(summary = "创建分享", description = "创建笔记分享")
    @LogRecord(description = "创建笔记分享", module = "分享管理", operationType = LogRecord.OperationType.CREATE)
    public ResponseVo<ShareDTO> createShare(@Valid @RequestBody ShareCreateDTO dto) {
        Long userId = UserContext.getUserId();
        return ResponseVo.success(shareService.createShare(userId, dto));
    }
    
    @DeleteMapping("/api/v1/shares/{shareId}")
    @Operation(summary = "取消分享", description = "取消笔记分享")
    @LogRecord(description = "取消笔记分享", module = "分享管理", operationType = LogRecord.OperationType.DELETE)
    public ResponseVo<Boolean> cancelShare(
            @Parameter(description = "分享ID") @PathVariable Long shareId) {
        Long userId = UserContext.getUserId();
        return ResponseVo.success(shareService.cancelShare(userId, shareId));
    }
    
    @GetMapping("/share/{token}")
    @Operation(summary = "获取分享笔记", description = "通过分享链接访问笔记")
    @LogRecord(description = "获取分享详情", module = "分享管理", operationType = LogRecord.OperationType.QUERY)
    public ResponseVo<NoteDTO> getNoteByShareToken(
            @Parameter(description = "分享令牌") @PathVariable String token) {
        return ResponseVo.success(shareService.getNoteByShareToken(token));
    }
    
    @GetMapping("/api/v1/shares/user")
    @Operation(summary = "获取用户分享", description = "分页获取当前用户的分享列表")
    @LogRecord(description = "获取用户分享列表", module = "分享管理", operationType = LogRecord.OperationType.QUERY)
    public ResponseVo<Page<ShareDTO>> getUserShares(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") long current,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") long size) {
        Long userId = UserContext.getUserId();
        return ResponseVo.success(shareService.getUserShares(userId, current, size));
    }
    
    @GetMapping("/api/v1/shares/note/{noteId}")
    @Operation(summary = "获取笔记分享", description = "分页获取指定笔记的分享列表")
    public ResponseVo<Page<ShareDTO>> getNoteShares(
            @Parameter(description = "笔记ID") @PathVariable Long noteId,
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") long current,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") long size) {
        Long userId = UserContext.getUserId();
        return ResponseVo.success(shareService.getNoteShares(userId, noteId, current, size));
    }
    
    @GetMapping("/api/v1/shares/check/{token}")
    @Operation(summary = "检查分享权限", description = "检查分享链接是否有效及权限类型")
    public ResponseVo<ShareDTO> checkSharePermission(
            @Parameter(description = "分享令牌") @PathVariable String token,
            @Parameter(description = "是否需要写权限") @RequestParam(defaultValue = "false") boolean requireWrite) {
        ShareDTO shareDTO = shareService.checkSharePermission(token, requireWrite);
        if (shareDTO == null) {
            return ResponseVo.error("分享链接无效或权限不足");
        }
        return ResponseVo.success(shareDTO);
    }
} 